import 'package:craze/services/index.dart';
import 'package:craze/stores/index.dart';
import 'package:get_it/get_it.dart';
import 'ai_service.dart';

final GetIt getIt = GetIt.instance;

class ServiceLocator {
  static Future<void> init() async {
    // 注册服务
    getIt.registerSingleton<UserService>(UserService());
    getIt.registerSingleton<TeamService>(TeamService());
    getIt.registerSingleton<QuestionService>(QuestionService());
    getIt.registerSingleton<VideoService>(VideoService());
    getIt.registerSingleton<ShareService>(ShareService());
    getIt.registerSingleton<ChatService>(ChatService());
    getIt.registerSingleton<ScanResultService>(ScanResultService());
    getIt.registerSingleton<CollectionService>(CollectionService());
    getIt.registerSingleton<ShareCommentService>(ShareCommentService());

    // 注册AI服务
    getIt.registerSingleton<AIService>(AIService(getIt<ScanResultService>()));

    // 注册Store
    getIt.registerSingleton<AuthStore>(AuthStore());

    // 初始化AuthStore，恢复登录状态
    await getIt<AuthStore>().init();

    // 注册依赖于AuthStore的Store
    getIt.registerSingleton<TeamStore>(TeamStore(getIt<AuthStore>()));
    getIt.registerSingleton<QuestionStore>(QuestionStore(getIt<AuthStore>()));
    getIt.registerSingleton<VideoStore>(VideoStore(getIt<AuthStore>()));
    getIt.registerSingleton<ShareStore>(ShareStore(getIt<AuthStore>()));
    getIt.registerSingleton<ChatStore>(ChatStore(getIt<AuthStore>()));
    getIt.registerSingleton<MarineProtectionStore>(
      MarineProtectionStore(getIt<AuthStore>()),
    );
    getIt.registerSingleton<ScanResultStore>(
      ScanResultStore(getIt<AuthStore>()),
    );
    getIt.registerSingleton<CollectionStore>(
      CollectionStore(getIt<AuthStore>()),
    );
    getIt.registerSingleton<ShareCommentStore>(
      ShareCommentStore(getIt<AuthStore>()),
    );

    // 初始化示例数据
    await _initSampleData();
  }

  // 初始化一些示例数据方便开发和测试
  static Future<void> _initSampleData() async {
    // 检查是否已有用户数据，如果有则不再创建示例数据
    final userService = getIt<UserService>();
    final existingUsers = userService.getAllUsers();

    if (existingUsers.isNotEmpty) {
      // 已有数据，不需要创建示例数据
      return;
    }

    // 添加默认用户数据
    await userService.createSampleUsers();

    // 获取用户ID列表用于创建其他示例数据
    final users = userService.getAllUsers();
    final userIds = users.map((user) => user.id).toList();

    // 添加示例聊天和消息数据
    final chatService = getIt<ChatService>();
    await chatService.createSampleChatsAndMessages(userIds);

    // 添加示例团队数据
    final teamService = getIt<TeamService>();
    await teamService.createSampleTeams(userIds);

    // 添加示例问答数据
    final questionService = getIt<QuestionService>();
    await questionService.createSampleQuestionsAndAnswers(userIds);

    // 添加示例扫描结果数据
    final scanResultService = getIt<ScanResultService>();
    await scanResultService.createSampleScanResults(userIds);

    // 添加示例视频数据
    final videoStore = getIt<VideoStore>();
    await videoStore.createSampleVideos();

    // 添加示例分享数据
    final shareStore = getIt<ShareStore>();
    await shareStore.createSampleShares();

    // 初始化海洋保护活动和表情
    final marineStore = getIt<MarineProtectionStore>();
    await marineStore.initializeDefaultActivities();

    // 创建示例收藏数据
    final collectionService = getIt<CollectionService>();
    await collectionService.createSampleCollections();
  }

  // 重置并重新初始化所有数据（开发测试用）
  static Future<void> reinitializeAllData() async {
    // 登出当前用户
    final authStore = getIt<AuthStore>();
    authStore.logout();

    // 清空所有数据
    await HiveService.resetAllData();

    // 重新初始化示例数据
    await _initSampleData();

    // 刷新所有Store的数据
    getIt<TeamStore>().loadTeams();
    getIt<QuestionStore>().loadQuestions();
    getIt<VideoStore>().loadVideos();
    getIt<ShareStore>().loadShares();
    getIt<ChatStore>().loadUserChats();
    getIt<ScanResultStore>().loadScanResults();
    getIt<MarineProtectionStore>().loadActivities();
    getIt<MarineProtectionStore>().loadEmojis();
    getIt<CollectionStore>().loadUserCollections();
  }
}
